package com.attendance.service.impl;

import com.attendance.common.CommonPage;
import com.attendance.common.CommonResult;
import com.attendance.mapper.TeacherMapper;
import com.attendance.pojo.Pages;
import com.attendance.pojo.Teacher;
import com.attendance.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WYH on 2022/4/16 15:00
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Teacher> selectAll() {
        return teacherMapper.selectAll();
    }

    @Override
    public CommonResult selectTeacher(Teacher teacher, Pages pages) {
        PageHelper.startPage(pages.getPage(), pages.getNum());
        List<Teacher> teachers = teacherMapper.selectTeacher(teacher);
        if (teachers.size()==0){
            return CommonResult.success("暂无数据");
        }else {
            return CommonResult.success(CommonPage.restPage(teachers),"查询成功！");
        }
    }

    @Override
    public CommonResult addTeacher(Teacher teacher) {
        String str = teacher.getTeaIdentity();
        String pwd = str.substring(12);
        teacher.setTeaPwd(pwd);
        int i = teacherMapper.addTeacher(teacher);
        if (i==1){
            return CommonResult.success("新增信息成功！");
        }else {
            return CommonResult.failed("新增信息失败！");
        }
    }

    @Override
    public Teacher selectByTeaId(String id) {
        return teacherMapper.selectByTeaId(id);
    }

    @Override
    public CommonResult updateTeacher(Teacher teacher) {
        int i = teacherMapper.updateTeacher(teacher);
        if (i==1){
            return CommonResult.success("修改信息成功！");
        }else {
            return CommonResult.failed("修改数据失败！");
        }
    }

    @Override
    public CommonResult modifyStatus(String id) {
        int i = teacherMapper.modifyStatus(id);
        if (i==1){
            return CommonResult.success("修改状态成功！");
        }else {
            return CommonResult.failed("修改状态失败！");
        }
    }

    @Override
    public Teacher selectByTeaName(String Name) {
        return teacherMapper.selectByTeaName(Name);
    }
}
